package com.icangya.Utils.exportFile;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.icangya.Utils.FormatUtil;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class CreateSimpleExcelToDisk
{
    /**
     * @功能：手工构建一个简单格式的Excel
     */
    private static List<Student> getStudent() throws Exception
    {
        List list = new ArrayList();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");

        Student user1 = new Student(1, "张三", 16, df.parse("1997-03-12"));
        Student user2 = new Student(2, "李四", 17, df.parse("1996-08-12"));
        Student user3 = new Student(3, "王五", 26, df.parse("1985-11-12"));
        list.add(user1);
        list.add(user2);
        list.add(user3);

        return list;
    }

    //原始方法
//    public static void main(String[] args) throws Exception
//    {
//        // 第一步，创建一个webbook，对应一个Excel文件
//        HSSFWorkbook wb = new HSSFWorkbook();
//        // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
//        HSSFSheet sheet = wb.createSheet("学生表一");
//        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
//        HSSFRow row = sheet.createRow((int) 0);
//        // 第四步，创建单元格，并设置值表头 设置表头居中
//        HSSFCellStyle style = wb.createCellStyle();
//        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
//
//        HSSFCell cell = row.createCell((short) 0);
//        cell.setCellValue("学号");
//        cell.setCellStyle(style);
//        cell = row.createCell((short) 1);
//        cell.setCellValue("姓名");
//        cell.setCellStyle(style);
//        cell = row.createCell((short) 2);
//        cell.setCellValue("年龄");
//        cell.setCellStyle(style);
//        cell = row.createCell((short) 3);
//        cell.setCellValue("生日");
//        cell.setCellStyle(style);
//
//        // 第五步，写入实体数据 实际应用中这些数据从数据库得到，
//        List list = CreateSimpleExcelToDisk.getStudent();
//
//        for (int i = 0; i < list.size(); i++)
//        {
//            row = sheet.createRow((int) i + 1);
//            Student stu = (Student) list.get(i);
//            // 第四步，创建单元格，并设置值
//            row.createCell((short) 0).setCellValue((double) stu.getId());
//            row.createCell((short) 1).setCellValue(stu.getName());
//            row.createCell((short) 2).setCellValue((double) stu.getAge());
//            cell = row.createCell((short) 3);
//            cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu
//                    .getBirth()));
//        }
//        // 第六步，将文件存到指定位置
//        try
//        {
//            FileOutputStream fout = new FileOutputStream("E:/students.xls");
//            wb.write(fout);
//            fout.close();
//        }
//        catch (Exception e)
//        {
//            e.printStackTrace();
//        }
//    }


    public static String exportExcel(List<GoodOrder> goodOrderList) throws Exception
    {
        // 第一步，创建一个webbook，对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("订单表");
        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        HSSFRow row = sheet.createRow((int) 0);
        // 第四步，创建单元格，并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式


        HSSFCell cell = row.createCell((short) 0);
        cell.setCellValue("ID");
        cell.setCellStyle(style);
        cell = row.createCell((short) 1);
        cell.setCellValue("orderId订单号");
        cell.setCellStyle(style);
        cell = row.createCell((short) 2);
        cell.setCellValue("price商品单价");
        cell.setCellStyle(style);
        cell = row.createCell((short) 3);
        cell.setCellValue("sumPices数量");
        cell.setCellStyle(style);
        cell = row.createCell((short) 4);
        cell.setCellValue("remark备注");
        cell.setCellStyle(style);
        cell = row.createCell((short) 5);
        cell.setCellValue("numPiece订单数量");
        cell.setCellStyle(style);
        cell = row.createCell((short) 6);
        cell.setCellValue("createTime创建时间");
        cell.setCellStyle(style);
        cell = row.createCell((short) 7);
        cell.setCellValue("userid用户id");
        cell.setCellStyle(style);
        cell = row.createCell((short) 8);
        cell.setCellValue("openid用户openid");
        cell.setCellStyle(style);
        cell = row.createCell((short) 9);
        cell.setCellValue("paystatus是否已付款");
        cell.setCellStyle(style);
        cell = row.createCell((short) 10);
        cell.setCellValue("sendStatus是否已发货");
        cell.setCellStyle(style);
        cell = row.createCell((short) 11);
        cell.setCellValue("mainPic订单主图");
        cell.setCellStyle(style);
        cell = row.createCell((short) 12);
        cell.setCellValue("orderUserNickName用户昵称");
        cell.setCellStyle(style);
        cell = row.createCell((short) 13);
        cell.setCellValue("orderUserImg用户头像");
        cell.setCellStyle(style);
        cell = row.createCell((short) 14);
        cell.setCellValue("orderAddress订单地址");
        cell.setCellStyle(style);
        cell = row.createCell((short) 15);
        cell.setCellValue("addressId地址id");
        cell.setCellStyle(style);
        cell = row.createCell((short) 16);
        cell.setCellValue("mobilePhone用户手机号码");
        cell.setCellStyle(style);
        cell = row.createCell((short) 17);
        cell.setCellValue("intro商品简介");
        cell.setCellStyle(style);
        cell = row.createCell((short) 18);
        cell.setCellValue("expressNum快递号");
        cell.setCellStyle(style);
        cell = row.createCell((short) 19);
        cell.setCellValue("expressComp快递公司");
        cell.setCellStyle(style);

        // 第五步，写入实体数据 实际应用中这些数据从数据库得到，
//        List list = CreateSimpleExcelToDisk.getStudent();
        List list = goodOrderList;
        for (int i = 0; i < list.size(); i++)
        {
            row = sheet.createRow((int) i + 1);
            GoodOrder stu = (GoodOrder) list.get(i);
            // 第四步，创建单元格，并设置值
            row.createCell((short) 0).setCellValue( stu.getId());
            row.createCell((short) 1).setCellValue(stu.getOrderId());
            row.createCell((short) 2).setCellValue(stu.getPrice());
            row.createCell((short) 3).setCellValue(stu.getSumPices());
            row.createCell((short) 4).setCellValue(stu.getRemark());
            row.createCell((short) 5).setCellValue(stu.getNumPiece());
            row.createCell((short) 6).setCellValue(stu.getCreateTime());
            row.createCell((short) 7).setCellValue(stu.getUserid());
            row.createCell((short) 8).setCellValue(stu.getOpenid());
            row.createCell((short) 9).setCellValue(stu.getPaystatus());
            row.createCell((short) 10).setCellValue(stu.getSendStatus());
//            row.createCell((short) 9).setCellValue("0".equals(stu.getPaystatus())?"未付款":"已付款");
//            row.createCell((short) 10).setCellValue("0".equals(stu.getSendStatus())?"未发货":"已发货");
            row.createCell((short) 11).setCellValue(stu.getMainPic());
            row.createCell((short) 12).setCellValue(stu.getOrderUserNickName());
            row.createCell((short) 13).setCellValue(stu.getOrderUserImg());
            row.createCell((short) 14).setCellValue(stu.getOrderAddress());
            row.createCell((short) 15).setCellValue(stu.getAddressId());
            row.createCell((short) 16).setCellValue(stu.getMobilePhone());
            row.createCell((short) 17).setCellValue(stu.getIntro());
            row.createCell((short) 18).setCellValue(stu.getExpressNum());
            row.createCell((short) 19).setCellValue(stu.getExpressComp());
            //        this.id = id;
            //        this.orderId = orderId;
            //        this.price = price;
            //        this.sumPices = sumPices;
            //        this.remark = remark;
            //        this.numPiece = numPiece;
            //        this.createTime = createTime;
            //        this.userid = userid;
            //        this.openid = openid;
            //        this.paystatus = paystatus;
            //        this.sendStatus = sendStatus;
            //        this.mainPic = mainPic;
            //        this.orderUserNickName = orderUserNickName;
            //        this.orderUserImg = orderUserImg;
            //        this.orderAddress = orderAddress;
            //        this.addressId = addressId;
            //        this.mobilePhone = mobilePhone;
            //        this.intro = intro;
            //        this.expressNum = expressNum;
            //        this.expressComp = expressComp;
        }
        // 第六步，将文件存到指定位置
        String excelPath = "/upload/orderExcel/"+ FormatUtil.getStringDate();

        File f = new File("/data/project/redwineshop"+excelPath);
        if (!f.exists()){
            f.mkdirs();
        }
        try{
            FileOutputStream fout = new FileOutputStream("/data/project/redwineshop"+excelPath + "/goodsOrder.xls");
            wb.write(fout);
            fout.close();
            return excelPath + "/goodsOrder.xls";
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }
}